package com.common;

import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * description
 * @author gg
 *
 */
public final class JdbcUtils
{
    private static SqlSessionFactory factory = null;

    private static SqlSessionFactory getSessionFactory()
    {
        if (null == factory)
        {
            String resource = "mybatis-config.xml";
            Reader reader = null;

            try
            {
                reader = Resources.getResourceAsReader(resource);
            }
            catch (Exception ex)
            {
            }
            factory = new SqlSessionFactoryBuilder().build(reader);
            if (null != reader)
            {
                try
                {
                    reader.close();
                }
                catch (IOException e)
                {
                }
            }
        }
        return factory;
    }

    /**
     * description 
     * 得到数据库连
     * @return void
     */
    public static SqlSession getSession()
    {
        SqlSessionFactory sessionFactory = JdbcUtils.getSessionFactory();
        SqlSession mysession = sessionFactory.openSession();
        return mysession;
    }

}
